package cn.hrlly.tao2shou.utils;

import android.content.Context;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;

import java.util.ArrayList;
import java.util.List;

import cn.hrlly.tao2shou.domain.Commodities;
import cn.hrlly.tao2shou.domain.CommodityList;
import cn.hrlly.tao2shou.domain.HistorySearch;
import cn.hrlly.tao2shou.domain.User;

/**
 * User: gengliang
 * Date: 2016-01-25
 * Time: 13:32
 */

public class DatabaseUtil {
    private static DatabaseHelper databaseHelper;
    private static SQLiteDatabase sqLiteDatabase;
    private static int i=0;

    public DatabaseUtil(Context context) {
        databaseHelper = new DatabaseHelper(context, "tao2shou.db", null, 1);
        sqLiteDatabase = databaseHelper.getWritableDatabase();
    }

    //关闭数据库
    public static void closeDatabase() {
        if (databaseHelper != null) {
            databaseHelper.close();
        }
    }

    //向用户数据表中插入数据
    public static void insertUserInfo(User user) {
        i++;
        String string = "insert into user_info values('" +
                i+"','"+
                user.getUserId() + "','" +
                user.getNickname() + "','" +
                user.getGender() + "','" +
                user.getSchool() + "','" +
                user.getHeadPortrait() + "','" +
                user.getPhoneNumber() + "')";
        sqLiteDatabase.execSQL(string);
    }

    //删除用户数据表中的数据
    public static void deleteUserInfo() {
        String string = "truncate user_info";
        sqLiteDatabase.execSQL(string);
    }

    //更改用户数据表中的数据
    public static void updateUserInfo(User user) {
        deleteUserInfo();
        insertUserInfo(user);
    }

    //查询用户数据表的数据
    public static User selectUserInfo() {
        String string = "select * from user_info";
        Cursor cursor = sqLiteDatabase.rawQuery(string, null);
        User user = null;
        while (cursor.moveToNext()) {
            user = new User();
            user.setUserId(cursor.getString(0));
            user.setNickname(cursor.getString(1));
            user.setGender(cursor.getString(2));
            user.setSchool(cursor.getString(3));
            user.setHeadPortrait(cursor.getString(4));
            user.setPhoneNumber(cursor.getString(5));
        }
        return user;

    }

    //向历史纪录表中插入数据
    public static void insertHistory(Commodities commodities) {
//        deleteHistory();
        ArrayList<Commodities> list = (ArrayList<Commodities>) selectHistory();
        boolean b = true;
        for (Commodities c : list) {
            if (c.getReleaseId().equals(commodities.getReleaseId())) {
                b = false;
            }
        }
        if (b) {
            i++;
            String string = "insert into history values('" +
                    i+"','"+
                    commodities.getReleaseId() + "','" +
                    commodities.getCommodityName() + "','" +
                    commodities.getCommodityPhoto() + "','" +
                    commodities.getCommodityPrice() + "')";
            sqLiteDatabase.execSQL(string);
        }
    }

    //删除历史记录表中的数据
    public static void deleteHistory() {
        String string = "delete from history";
        sqLiteDatabase.execSQL(string);
    }

    //查询历史记录表中的数据
    public static List<Commodities> selectHistory() {
        ArrayList<Commodities> lists = new ArrayList<Commodities>();
        String string = "select * from history";
        Cursor cursor = sqLiteDatabase.rawQuery(string, null);
        Commodities commodities = null;
        while (cursor.moveToNext()) {
            if (commodities == null)
                commodities = new Commodities();
            commodities.setReleaseId(cursor.getString(0));
            commodities.setCommodityName(cursor.getString(1));
            commodities.setCommodityPhoto(cursor.getString(2));
            commodities.setCommodityPrice(cursor.getFloat(3));
            lists.add(commodities);
        }
        return lists;
    }

    //向历史搜索表中插入数据
    public static void insertHistorySearch(HistorySearch historySearch) {
        i++;
        String string = "insert into history_search values('" +
                i+"','"+
                historySearch.getSearchName() + "')";
        sqLiteDatabase.execSQL(string);
    }

    //删除历史搜索表中的数据
    public static void deleteHistorySearch() {
        String string = "delete from history_search";
        sqLiteDatabase.execSQL(string);
    }

    //查询历史搜索表中的数据
    public static List<String> selectHistorySearch() {
        ArrayList<String> lists = null;
        String string = "select * from history_search";
        Cursor cursor = sqLiteDatabase.rawQuery(string, null);
        while (cursor.moveToNext()) {
            if (lists == null)
                lists = new ArrayList<String>();
            lists.add(cursor.getString(0));
        }
        return lists;
    }

}
